package com.googlecode.agscrum.controller.spring;

import java.util.HashSet;
import java.util.Set;

import org.acegisecurity.context.SecurityContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.googlecode.agscrum.model.entity.Product;
import com.googlecode.agscrum.model.entity.Resource;
import com.googlecode.agscrum.model.entity.User;
import com.googlecode.agscrum.model.service.UserService;

@Component("myIdentity")
@Scope(value = "session")
public class MyIdentity {
	private User user;

	private String login;

	private Product product = new Product();

	@Autowired
	@Qualifier("userService")
	private UserService userService;

	private Set<Resource> resources = new HashSet<Resource>();

	private boolean selectedProduct;

	// public User getUser() {
	// if (user == null) {
	// String login = SecurityContextHolder.getContext()
	// .getAuthentication().getName();
	// user = userService.searchByUserLogin(login);
	// for (Role role : user.getRoles()) {
	// for (Resource resource : role.getResources()) {
	// resources.add(resource);
	// }
	// }
	// }
	// return user;
	// }
	//
	// public boolean isAccess(String resource) {
	// if (getUser() != null) {
	// return resources.contains(new Resource(resource));
	// }
	// return false;
	// }
	//
	// public boolean isAvailable(String path) {
	// if (getUser() != null) {
	// for (Resource resource : resources) {
	// if (resource.getPath() != null
	// && resource.getPath().trim().contains(path.trim())) {
	// return true;
	// }
	// }
	// }
	// return false;
	// }
	public String getLogin() {
		return SecurityContextHolder.getContext().getAuthentication().getName();
	}

	public void selectProduct(boolean selectedProduct) {
		this.selectedProduct = selectedProduct;
	}

	public void setProduct(Product product) {
		selectProduct(true);
		this.product = product;
	}

	public boolean isSelectedProduct() {
		return selectedProduct;
	}

	public Product getProduct() {
		return product;
	}

}
